package com.wn.login.repository

import com.wn.common.model.User
import com.wn.network.api.ApiInterface
import com.wn.network.repository.BaseRepository
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2025/7/1 17:21
 * @Description: 登陆 仓库
 */
@ViewModelScoped
class LoginRepository @Inject constructor(private val api: ApiInterface) : BaseRepository() {
  /**
   * 登录
   * @param scope CoroutineScope
   * @param errorCall Function2<Int?, String?, Unit>?
   * @param showLoading Function1<Boolean, Unit>?
   * @param username String
   * @param password String
   * @return StateFlow<User?>
   */
  fun login(
    scope: CoroutineScope,
    errorCall: ((Int?, String?) -> Unit)? = null,
    showLoading: ((Boolean) -> Unit)? = null,
    username: String,
    password: String,
  ): StateFlow<User?> = callStateFlow(scope, errorCall, showLoading) {
    api.login(username, password)
  }

  /**
   * 注册
   * @param scope CoroutineScope
   * @param errorCall Function2<Int?, String?, Unit>?
   * @param showLoading Function1<Boolean, Unit>?
   * @param username String
   * @param password String
   * @param repassword String
   * @return StateFlow<User?>
   */
  fun register(
    scope: CoroutineScope,
    errorCall: ((Int?, String?) -> Unit)? = null,
    showLoading: ((Boolean) -> Unit)? = null,
    username: String,
    password: String,
    repassword: String,
  ): StateFlow<User?> = callStateFlow(scope, errorCall, showLoading) {
    api.register(username, password, repassword)
  }
}